package com.alibaba.nacos.example.spring.cloud.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @param <T>
 */
public class Pager<T> implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Integer pageSize;

	private Integer pageIndex;

	private Integer totalRecords;

	private Integer totalPages;

	private List<T> list = new ArrayList<T>();

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPageIndex() {
		if (pageIndex <= 0) {
			return 1;
		}
		return pageIndex;
	}

	public void setPageIndex(Integer pageIndex) {
		if (pageIndex <= 0) {
			this.pageIndex = 1;
		}
		this.pageIndex = pageIndex;
	}

	public Integer getTotalRecords() {
		return totalRecords;
	}

	public void setTotalRecords(Integer totalRecords) {
		this.totalRecords = totalRecords;
	}

	public Integer getTotalPages() {
		return totalPages;
	}

	public void setTotalPages() {
		this.totalPages = this.totalRecords % this.pageSize == 0 ? this.totalRecords / this.pageSize
				: this.totalRecords / this.pageSize + 1;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public boolean getIsFirst() {
		if (this.pageIndex == 1) {
			return true;
		} else {
			return false;
		}
	}

	public boolean getIsLast() {
		if (this.pageIndex >= this.totalPages) {
			return true;
		} else {
			return false;
		}
	}

	public boolean hasPrevious() {
		if (this.pageIndex > 1 && this.pageIndex <= this.totalPages) {
			return true;
		} else {
			return false;
		}
	}

	public boolean hasNext() {
		if (this.pageIndex < this.totalPages) {
			return true;
		} else {
			return false;
		}
	}

	public Integer getFirstPage() {
		return 1;
	}

	public Integer getLastPage() {
		return this.totalPages;
	}

	public Integer getPreviousPage() {
		if (this.hasPrevious()) {
			return this.pageIndex - 1;
		} else {
			return 1;
		}
	}

	public Integer getNextPage() {
		if (this.hasNext()) {
			return this.pageIndex + 1;
		} else {
			return this.totalPages;
		}
	}
}
